package il.ac.braude.sona.gui.infogui;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.gui.SONAFrame;

import java.awt.Color;
import java.awt.GradientPaint;
import java.util.HashMap;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;

/**
 * Class for extensions report, creates the panel for the report and adds it to the frame
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * Laboratory in Software Engineering,Ort Braude College.</p> 
 * 
 */
@SuppressWarnings("serial")
public class ExtensionGraphFrame extends SONAFrame {

    /**
     * Constructor, creates the panel and the values for the graph and adds it to the frame
     *
     * @param reportName  the frame title.
     * @param reportAnswer - all information about the report
     */
    public ExtensionGraphFrame(final String reportName, HashMap<String, String> reportAnswer) {

        super();
        final CategoryDataset dataset = createDataset(reportName,reportAnswer);
        final JFreeChart chart = createChart(dataset,reportName);
        final ChartPanel chartPanel = new ChartPanel(chart);
        this.add(chartPanel);

    }

  /**
   * creates the dataset to be implemented in the graph
   * @param reportName - the report name
   * @param reportAnswer - the report fields and values
   * @return the created dataset
   */
    private CategoryDataset createDataset(String reportName, HashMap<String, String> reportAnswer) {
        
    	final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    		final String sum = "Extension days approved";
	        final String num = "Number of approved extensions";
	        final String ret = "Returned requests";
	        final String days = "Days added";
	        final String category1 = "";
	        
	        dataset.addValue(Integer.parseInt(reportAnswer.get(sum)), sum, category1);
	        dataset.addValue(Integer.parseInt(reportAnswer.get(num)), num, category1);
	        dataset.addValue(Integer.parseInt(reportAnswer.get(ret)), ret, category1);
	        dataset.addValue(Integer.parseInt(reportAnswer.get(days)), days, category1);
        // create the dataset...
        return dataset;
        
    }
    
    /**
     * Creates the bar chart by the dataset 
     * and sets optional configurations for the chart
     * @param dataset  the dataset to be used in the graph.
     * @param reportName fields and values of the report
     * 
     * @return The created chart.
     */
    private JFreeChart createChart(final CategoryDataset dataset, String reportName) {
        
        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart3D(
            reportName,         // chart title
            "Category",               // domain axis label
            null,                     // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            false,                     // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        
       LegendTitle legend = new LegendTitle(chart.getPlot());
       legend.setPosition(RectangleEdge.RIGHT);
       chart.addSubtitle(legend);
     
        final CategoryAxis axis = plot.getDomainAxis();
        axis.setLabel("Extensions data for all information systems");
        axis.setTickLabelsVisible(false);
      
        

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
        // set up gradient paints for series...
        final GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f, Color.blue, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp1 = new GradientPaint(
            0.0f, 0.0f, Color.green, 
            0.0f, 0.0f, Color.lightGray
        );
        final GradientPaint gp2 = new GradientPaint(
            0.0f, 0.0f, Color.red, 
            0.0f, 0.0f, Color.lightGray
        );
         final GradientPaint gp3 = new GradientPaint(
                    0.0f, 0.0f, Color.MAGENTA, 
                    0.0f, 0.0f, Color.lightGray
        );
         final GradientPaint gp4 = new GradientPaint(
                 0.0f, 0.0f, Color.orange, 
                 0.0f, 0.0f, Color.lightGray
     );
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        renderer.setSeriesPaint(2, gp2);
        renderer.setSeriesPaint(3, gp3);
        renderer.setSeriesPaint(4, gp4);
        renderer.setBaseItemLabelFont(Resources.Futura18);
        renderer.setMaxBarWidth(0.1);
        
        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 10.0)
        );
        domainAxis.setLabelFont(Resources.Futura18);
        
//        // OPTIONAL CUSTOMISATION COMPLETED.
        return chart;   
    }
}
